//
//  LibraryAppViewController_iPhone.m
//  Pepperdine
//
//  Created by Michael Coats on 3/13/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MapAppPDFViewController_iPhone.h"

@implementation MapAppPDFViewController_iPhone

@synthesize webView, buildingName, pdfName, request, toolbar, pdfToolbarLabel;


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)];
	webView.scalesPageToFit = YES;
	webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
	//webView.delegate = self;
	[self.view addSubview:webView];
	[self switchPDF:pdfName];	
	
//	NSLog(@"%@", buildingName); 
//	NSString *path;
//	if (("%@", buildingName) == @"KSC (Keck Science Center)") {
//	path = [[NSBundle mainBundle] pathForResource:@"KSC-Floor1" ofType:@"pdf"];
//	} else if (buildingName == @"CAC (Cultural Arts Center)") {
//	path = [[NSBundle mainBundle] pathForResource:@"CAC-Floor1" ofType:@"pdf"];
//	} else {
//	path = [[NSBundle mainBundle] pathForResource:@"Dorm-Floor1" ofType:@"pdf"];
//	}
//	NSURL *targetURL = [NSURL fileURLWithPath:path];
//	NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
//	[webView loadRequest:request];
//	
//	[self.view addSubview:webView];
//	[webView release];
	
	toolbar = [[UIToolbar alloc] init];
	toolbar.barStyle = UIBarStyleDefault;
	// set the frame size to match the size of the default navigation bar, 44 pixels
	toolbar.frame = CGRectMake(0, 436, 320, 44);
	
	
	// add toolbar as a subview, in final app it might look more like: 
	[self.navigationController.view addSubview:toolbar];
	//[self.view addSubview:toolbar];
	
	
	[self updateToolbar:pdfToolbarLabel];
	
	
	//UIBarButtonItem *customItem1 = [[UIBarButtonItem alloc] initWithTitle:@"Up" style:UIBarButtonItemStyleBordered target:self action:@selector(floorUp)];
//	UIBarButtonItem *customItem2 = [[UIBarButtonItem alloc] initWithTitle:@"Down" style:UIBarButtonItemStyleBordered target:self action:@selector(floorDown)];
//	
//	UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 202, 23)];
//	label.textAlignment = UITextAlignmentCenter;
//	label.backgroundColor = [UIColor clearColor];
//    label.text = pdfName;
//	[label setTextColor:[UIColor whiteColor]];
//    label.font = [UIFont boldSystemFontOfSize:20.0];
//    UIBarButtonItem *toolBarTitle = [[UIBarButtonItem alloc] initWithCustomView:label];
//    [label release];
//		
//	NSArray *items = [NSArray arrayWithObjects: customItem1, customItem2, toolBarTitle, nil];
//	[toolbar setItems:items animated:NO];
//	[customItem1 release];
//	[customItem2 release];
//	[toolBarTitle release];
}


/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */


- (void) floorUp {
	NSLog(@"floorUp");
	if ([pdfName isEqualToString:@"RAC-Floor1"]) {
		buildingName = @"AC (Appleby Center)";
		pdfName = @"AC-Floor2";
		pdfToolbarLabel = @"Floor 2";
	} else if ([pdfName isEqualToString:@"CAC-Floor1"]) {
		pdfName = @"CAC-Floor2";
		pdfToolbarLabel = @"Floor 2";
	} else if ([pdfName isEqualToString:@"CAC-Floor2"]) {
		pdfName = @"CAC-Floor3";
		pdfToolbarLabel = @"Floor 3";
	} else if ([pdfName isEqualToString:@"CAC-Floor3"]) {
		pdfName = @"CAC-Floor4";
		pdfToolbarLabel = @"Floor 4";
	} else if ([pdfName isEqualToString:@"KSC-Floor1"]) {
		pdfName = @"KSC-Floor2";
		pdfToolbarLabel = @"Floor 2";
	} else if ([pdfName isEqualToString:@"KSC-Floor2"]) {
		pdfName = @"KSC-Floor3";
		pdfToolbarLabel = @"Floor 3";
	} else if ([pdfName isEqualToString:@"KSC-Floor3"]) {
		pdfName = @"KSC-Floor4";
		pdfToolbarLabel = @"Floor 4";
	} else if ([pdfName isEqualToString:@"Dorm-Floor1"]) {
		pdfName = @"Dorm-Floor2";
		pdfToolbarLabel = @"Floor 2";
	} else if ([pdfName isEqualToString:@"Alpha-Floor1"]) {
		pdfName = @"Alpha-Floor2";
		pdfToolbarLabel = @"Floor 2";
	} else {
		//do nothing
	}

	[self switchPDF:pdfName];
	[self updateToolbar:pdfToolbarLabel];
}

- (void) floorDown {
	NSLog(@"floorDown");
	if ([pdfName isEqualToString:@"AC-Floor2"]) {
		buildingName = @"RAC (Rockwell Academic Center)";
		pdfName = @"RAC-Floor1";
		pdfToolbarLabel = @"Floor 1";
	} else if ([pdfName isEqualToString:@"CAC-Floor4"]) {
		pdfName = @"CAC-Floor3";
		pdfToolbarLabel = @"Floor 3";
	} else if ([pdfName isEqualToString:@"CAC-Floor3"]) {
		pdfName = @"CAC-Floor2";
		pdfToolbarLabel = @"Floor 2";
	} else if ([pdfName isEqualToString:@"CAC-Floor2"]) {
		pdfName = @"CAC-Floor1";
		pdfToolbarLabel = @"Floor 1";
	} else if ([pdfName isEqualToString:@"KSC-Floor4"]) {
		pdfName = @"KSC-Floor3";
		pdfToolbarLabel = @"Floor 3";
	} else if ([pdfName isEqualToString:@"KSC-Floor3"]) {
		pdfName = @"KSC-Floor2";
		pdfToolbarLabel = @"Floor 2";
	} else if ([pdfName isEqualToString:@"KSC-Floor2"]) {
		pdfName = @"KSC-Floor1";
		pdfToolbarLabel = @"Floor 1";
	} else if ([pdfName isEqualToString:@"Dorm-Floor2"]) {
		pdfName = @"Dorm-Floor1";
		pdfToolbarLabel = @"Floor 1";
	} else if ([pdfName isEqualToString:@"Alpha-Floor2"]) {
		pdfName = @"Alpha-Floor1";
		pdfToolbarLabel = @"Floor 1";
	} else {
		//do nothing
	}
	
	[self switchPDF:pdfName];
	[self updateToolbar:pdfToolbarLabel];
}


- (void) switchPDF: (NSString *)name {
	NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"pdf"];
	NSURL *targetURL = [NSURL fileURLWithPath:path];
	request = [NSURLRequest requestWithURL:targetURL];
	[webView loadRequest:request];
}


- (void) updateToolbar: (NSString *)newTitle {
	// Update Title (mainly for AC floor 2 to RAC floor 1)
	self.title = buildingName;
	
	// Get up and down arrow images from the main bundle
	NSString *path = [[NSBundle mainBundle] pathForResource:@"UpArrow" ofType:@"png"];
	UIImage *upButtonImage = [UIImage imageWithContentsOfFile:path];
	path = [[NSBundle mainBundle] pathForResource:@"DownArrow" ofType:@"png"];
	UIImage *downButtonImage = [UIImage imageWithContentsOfFile:path];
	
	// Create temporary buttons, and set their views to the arrow images
	UIButton *upButton = [UIButton buttonWithType:UIButtonTypeCustom];
	UIButton *downButton = [UIButton buttonWithType:UIButtonTypeCustom];
	[upButton setImage:upButtonImage forState:UIControlStateNormal];
	[downButton setImage:downButtonImage forState:UIControlStateNormal];
	upButton.frame = CGRectMake(0, 0, 30, 30);
	downButton.frame = CGRectMake(0, 0, 30, 30);
	
	// Create bar button items, and set their actions
	UIBarButtonItem *customItem1 = [[UIBarButtonItem alloc] initWithCustomView:upButton];
	UIBarButtonItem *customItem2 = [[UIBarButtonItem alloc] initWithCustomView:downButton];
	[upButton addTarget:self action:@selector(floorUp) forControlEvents:UIControlEventTouchUpInside];
	[downButton addTarget:self action:@selector(floorDown) forControlEvents:UIControlEventTouchUpInside];
	
	// Create the label to display the floor
	UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 220, 30)];
	label.textAlignment = UITextAlignmentCenter;
	label.backgroundColor = [UIColor clearColor];
    label.text = newTitle;
	[label setTextColor:[UIColor whiteColor]];
    label.font = [UIFont boldSystemFontOfSize:20.0];
    UIBarButtonItem *toolBarTitle = [[UIBarButtonItem alloc] initWithCustomView:label];
    [label release];
	
	// Set the up and down buttons and label as toolbar items
	NSArray *items = [NSArray arrayWithObjects: customItem1, toolBarTitle, customItem2, nil];
	[toolbar setItems:items animated:NO];
	[customItem1 release];
	[customItem2 release];
	[toolBarTitle release];
}



// catch the event when 
-(void)eventHandler: (NSNotification *) notification
{	
	buildingName = notification.object;
	self.title = buildingName;
	
	if ([buildingName isEqualToString:@"AC (Appleby Center)"]) {
		pdfName = @"AC-Floor2";
		pdfToolbarLabel = @"Floor 2";
	}
	else if ([buildingName isEqualToString:@"Krown Alpha"]) {
		pdfName = @"Alpha-Floor1";
		pdfToolbarLabel = @"Floor 1";
	}
	else if ([buildingName isEqualToString:@"CAC (Cultural Arts Center)"]) {
		pdfName = @"CAC-Floor1";
		pdfToolbarLabel = @"Floor 1";
	}
	else if ([buildingName isEqualToString:@"KSC (Keck Science Center)"]) {
		pdfName = @"KSC-Floor1";
		pdfToolbarLabel = @"Floor 1";
	}
	else if ([buildingName isEqualToString:@"RAC (Rockwell Academic Center)"]) {
		pdfName = @"RAC-Floor1";
		pdfToolbarLabel = @"Floor 1";
	}
	else if ([buildingName isEqualToString:@"Lindhurst Theater"]) {
		pdfName = @"CAC-Floor2";
		pdfToolbarLabel = @"Floor 2";
	}
	else if ([buildingName isEqualToString:@"Banowsky"]) {
		pdfName = @"Dorm-Floor1";
		pdfToolbarLabel = @"Floor 1";
	}
	else if ([buildingName isEqualToString:@"Conner"]) {
		pdfName = @"Dorm-Floor1";
		pdfToolbarLabel = @"Floor 1";
	}
	else if ([buildingName isEqualToString:@"Crocker"]) {
		pdfName = @"Dorm-Floor1";
		pdfToolbarLabel = @"Floor 1";
	}
	else if ([buildingName isEqualToString:@"Darnell"]) {
		pdfName = @"Dorm-Floor1";
		pdfToolbarLabel = @"Floor 1";
	}
	else if ([buildingName isEqualToString:@"DeBell"]) {
		pdfName = @"Dorm-Floor1";
		pdfToolbarLabel = @"Floor 1";
	}
	else if ([buildingName isEqualToString:@"Dewey"]) {
		pdfName = @"Dorm-Floor1";
		pdfToolbarLabel = @"Floor 1";
	}
	else if ([buildingName isEqualToString:@"Eaton"]) {
		pdfName = @"Dorm-Floor1";
		pdfToolbarLabel = @"Floor 1";
	}
	else if ([buildingName isEqualToString:@"Fifield"]) {
		pdfName = @"Dorm-Floor1";
		pdfToolbarLabel = @"Floor 1";
	}
	else if ([buildingName isEqualToString:@"Hayes"]) {
		pdfName = @"Dorm-Floor1";
		pdfToolbarLabel = @"Floor 1";
	}
	else if ([buildingName isEqualToString:@"Knott"]) {
		pdfName = @"Dorm-Floor1";
		pdfToolbarLabel = @"Floor 1";
	}
	else if ([buildingName isEqualToString:@"Miller"]) {
		pdfName = @"Dorm-Floor1";
		pdfToolbarLabel = @"Floor 1";
	}
	else if ([buildingName isEqualToString:@"Morgan"]) {
		pdfName = @"Dorm-Floor1";
		pdfToolbarLabel = @"Floor 1";
	}
	else if ([buildingName isEqualToString:@"Pauley"]) {
		pdfName = @"Dorm-Floor1";
		pdfToolbarLabel = @"Floor 1";
	}
	else if ([buildingName isEqualToString:@"E. Pengilly"]) {
		pdfName = @"Dorm-Floor1";
		pdfToolbarLabel = @"Floor 1";
	}
	else if ([buildingName isEqualToString:@"J. Pengilly"]) {
		pdfName = @"Dorm-Floor1";
		pdfToolbarLabel = @"Floor 1";
	}
	else if ([buildingName isEqualToString:@"Peppers"]) {
		pdfName = @"Dorm-Floor1";
		pdfToolbarLabel = @"Floor 1";
	}
	else if ([buildingName isEqualToString:@"Phillips"]) {
		pdfName = @"Dorm-Floor1";
		pdfToolbarLabel = @"Floor 1";
	}
	else if ([buildingName isEqualToString:@"Shafer"]) {
		pdfName = @"Dorm-Floor1";
		pdfToolbarLabel = @"Floor 1";
	}
	else if ([buildingName isEqualToString:@"Sigma"]) {
		pdfName = @"Dorm-Floor1";
		pdfToolbarLabel = @"Floor 1";
	}
	else if ([buildingName isEqualToString:@"White"]) {
		pdfName = @"Dorm-Floor1";
		pdfToolbarLabel = @"Floor 1";
	}
	else {
		pdfName = @"MissingFloorPlan";
		pdfToolbarLabel = @"Under construction";
	}
	
	[self switchPDF:pdfName];
	[self updateToolbar:pdfToolbarLabel];
	
//	Big chunk of duplicated code that I put into a function (or two)  -Aaron
//	UIBarButtonItem *customItem1 = [[UIBarButtonItem alloc] initWithTitle:@"Up" style:UIBarButtonItemStyleBordered target:self action:@selector(floorUp)];
//	UIBarButtonItem *customItem2 = [[UIBarButtonItem alloc] initWithTitle:@"Down" style:UIBarButtonItemStyleBordered target:self action:@selector(floorDown)];
//	UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 202, 23)];
//	label.textAlignment = UITextAlignmentCenter;
//	label.backgroundColor = [UIColor clearColor];
//    label.text = pdfName;
//	[label setTextColor:[UIColor whiteColor]];
//    label.font = [UIFont boldSystemFontOfSize:20.0];
//    UIBarButtonItem *toolBarTitle = [[UIBarButtonItem alloc] initWithCustomView:label];
//    [label release];
//	
//	NSArray *items = [NSArray arrayWithObjects: customItem1, customItem2, toolBarTitle, nil];
//	[toolbar setItems:items animated:NO];
//	[customItem1 release];
//	[customItem2 release];
//	[toolBarTitle release];
//	
//	
//	self.title = buildingName;
//	NSString *path = [[NSBundle mainBundle] pathForResource:pdfName ofType:@"pdf"];
//	NSURL *targetURL = [NSURL fileURLWithPath:path];
//	request = [NSURLRequest requestWithURL:targetURL];
//	[webView loadRequest:request];
}

-(void)viewWillAppear:(BOOL)animated {
	toolbar.hidden = NO;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)dealloc {
    [super dealloc];
	[toolbar release];
}


@end
